package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.qdlc.p2p.biz.service.AccountBankService;
import com.qdlc.p2p.common.constant.BindingBankStatus;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.AccountBankDao;
import com.qdlc.p2p.dal.dao.DictDao;
import com.qdlc.p2p.dal.dto.AccountBank;
import com.qdlc.p2p.dal.dto.Dict;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.AccountBankModel;
import com.qdlc.p2p.dal.util.UserUtils;

@Service("accountBankService")
public class AccountBankServiceImpl implements AccountBankService {

    @Resource
    private AccountBankDao accountBankDao;

    @Resource
    private DictDao dictDao;

    @Override
    public List<AccountBank> list(long userId) {
        return accountBankDao.list(userId);
    }

    @Override
    public List<AccountBank> listAll(long userId) {
        return accountBankDao.listAll(userId);
    }

    @Override
    public AccountBank save(AccountBank bank) {
        bank.setAddTime(DateUtil.getNow());
        bank.setAddIp(Global.getIP());
        bank.setStatus(1);
        return accountBankDao.save(bank);
    }

    @Override
    public AccountBank update(AccountBank bank) {
        return accountBankDao.update(bank);
    }

    @Override
    public void disable(User user, String bankNo) {
        accountBankDao.disable(user.getUserId(), bankNo);
    }

    @Override
    public int count(long userId) {
        return accountBankDao.count(userId);
    }

    @Override
    public AccountBank find(long userId, String bankNo) {
        return accountBankDao.find(userId, bankNo);
    }

    @Override
    public PageDataList<AccountBankModel> accountBankList(AccountBankModel model, int pageNumber, int pageSize) {
        QueryParam param = QueryParam.getInstance().addPage(pageNumber, pageSize);
        if (StringUtil.isNotBlank(model.getSearchName())) {
            SearchFilter orFilter1 = new SearchFilter("bankNo", Operators.LIKE, model.getSearchName().trim());
            SearchFilter orFilter2 = new SearchFilter("user.realName", Operators.LIKE, model.getSearchName().trim());
            SearchFilter orFilter3 = new SearchFilter("user.userName", Operators.LIKE, model.getSearchName().trim());
            param.addOrFilter(orFilter1, orFilter2, orFilter3);
        } else {
            if (StringUtil.isNotBlank(model.getBankNo())) {
                param.addParam("bankNo", Operators.LIKE, model.getBankNo().trim());
            }
            if (StringUtil.isNotBlank(model.getUserName())) {
                param.addParam("user.userName", Operators.LIKE, model.getUserName().trim());
            }
            if (StringUtil.isNotBlank(model.getRealName())) {
                param.addParam("user.realName", Operators.LIKE, model.getRealName().trim());
            }
            if (model.getStatus() != 99) {
                param.addParam("status", model.getStatus());
            }
        }
        param.addOrder(OrderType.DESC, "id");
        PageDataList<AccountBank> pList = accountBankDao.findPageList(param);
        PageDataList<AccountBankModel> modelList = new PageDataList<AccountBankModel>();
        List<AccountBankModel> list = new ArrayList<AccountBankModel>();
        modelList.setPage(pList.getPage());
        if (pList.getList().size() > 0) {
            for (int i = 0; i < pList.getList().size(); i++) {
                AccountBank accountBank = pList.getList().get(i);
                AccountBankModel abm = AccountBankModel.instance(accountBank);
                User user = UserUtils.getUserById(accountBank.getUserId());
                abm.setUserName(user.getUserName());
                abm.setRealName(user.getRealName());
                abm.setProvinceStr(accountBank.getProvince());
                abm.setCityStr(accountBank.getCity());
                abm.setAreaStr(accountBank.getArea());
                list.add(abm);
            }
        }
        modelList.setList(list);
        return modelList;
    }

    @Override
    public AccountBank findByBankNo(String bankNo) {
        QueryParam param = QueryParam.getInstance().addParam("bankNo", bankNo);
        return accountBankDao.findByCriteriaForUnique(param);
    }

    @Override
    public String getBankNameByCardNo(long userId, String cardNo) {
        AccountBank accountBank = accountBankDao.find(userId, cardNo);
        if (null != accountBank) {
            Dict dict = dictDao.find(accountBank.getBank());
            return dict.getName();
        }
        return "";
    }

    /**
     * 获取用户绑定银行卡的状态
     *
     * @param userId
     * @return
     * @author linya 2016-01-18
     * @see BindingBankStatus
     */
    @Override
    public BindingBankStatus bindingBankStatus(long userId) {
        List<AccountBank> bankList = list(userId);
        if (CollectionUtils.isEmpty(bankList)) {
            return BindingBankStatus.NO_BANK;
        }
        for (AccountBank bank : bankList) {
            if (StringUtils.isNotEmpty(bank.getMobilephone())) {
                return BindingBankStatus.SUCCUESS;
            }
        }
        return BindingBankStatus.NO_PHONE;
    }
}
